#include <QApplication>
#include <QLabel>
#include <QVBoxLayout>
#include <QPushButton>
#include <QTimer>
#include <QWidget>

// 简化版的测试函数，模拟我们实现的加载状态和动画效果
class TestWidget : public QWidget {
    Q_OBJECT
public:
    TestWidget(QWidget *parent = nullptr)
        : QWidget(parent) {
        // 创建简单布局
        auto *layout = new QVBoxLayout(this);
        
        // 添加测试按钮（模拟generateTrendBtn）
        testButton = new QPushButton("📈 测试趋势图", this);
        testButton->setMinimumWidth(140);
        
        // 添加指标标签（模拟数据展示）
        valueLabel = new QLabel("0.0%", this);
        valueLabel->setStyleSheet("font-size: 24px; color: #28a745; font-weight: bold;");
        
        layout->addWidget(testButton);
        layout->addWidget(valueLabel);
        
        // 连接信号槽
        connect(testButton, &QPushButton::clicked, this, &TestWidget::simulateLoading);
    }
    
public slots:
    void simulateLoading() {
        // 显示加载状态
        QString originalText = testButton->text();
        testButton->setDisabled(true);
        testButton->setText("⏳ 加载中...");
        
        // 模拟数据加载延迟
        QTimer::singleShot(1000, [this, originalText]() {
            // 模拟数据更新
            updateValueWithAnimation(originalText);
        });
    }
    
    void updateValueWithAnimation(const QString &originalText) {
        // 简单动画逻辑
        double targetValue = 15.8;
        double currentValue = 0;
        int steps = 20;
        double increment = targetValue / steps;
        
        // 动画定时器
        QTimer *animationTimer = new QTimer(this);
        connect(animationTimer, &QTimer::timeout, [this, animationTimer, currentValue, increment, steps, targetValue, originalText]() mutable {
            static int currentStep = 0;
            currentStep++;
            
            if (currentStep <= steps) {
                double value = currentStep * increment;
                valueLabel->setText(QString("%1%").arg(value, 0, 'f', 1));
                
                // 根据值调整颜色
                if (value > 0) {
                    valueLabel->setStyleSheet("font-size: 24px; color: #28a745; font-weight: bold;");
                }
            } else {
                // 动画完成，恢复按钮状态
                animationTimer->stop();
                animationTimer->deleteLater();
                testButton->setText(originalText);
                testButton->setDisabled(false);
            }
        });
        
        animationTimer->start(30); // 每30ms更新一次
    }
    
private:
    QPushButton *testButton;
    QLabel *valueLabel;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    
    TestWidget widget;
    widget.setWindowTitle("Trend Analysis Test");
    widget.resize(400, 200);
    widget.show();
    
    return app.exec();
}

#include "test_trend_analysis.moc"